<template>
  <base-layout>
    <template #searchPanel>
      <search-panel
        :addPer="authConfig.add"
        :selectPer="authConfig.query"
        addTitle="新增供应商"
        @insert="add"
        @search="search"
        :showSearchButton="true"
        :showAddButton="true"
      >
        <job-shop v-model="form.storeId" placeholder="所属门店" />
        <el-input
          v-model="form.supplierName"
          clearable
          placeholder="请输入供应商名称"
          style="width: 200px"
        />
      </search-panel>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :width="160"
      >
        <template #action="{ row }">
          <i
            class="el-icon-edit"
            title="编辑"
            style="margin-left: 15px; cursor: pointer"
            v-permission="authConfig.edit"
            @click="handleDataUpdateEdit(row, '编辑')"
          ></i>
          <i
            class="el-icon-delete"
            title="删除"
            style="color: red; margin-left: 20px; cursor: pointer"
            v-permission="authConfig.del"
            @click="handleDataDelete(row)"
          ></i>
        </template>
      </data-table>
    </template>
  </base-layout>
</template>
<script>
import {
  dataList,
  details,
  addData,
  updateData,
  deleteData,
  changeStatus,
} from '@/api/pre-sale/supplierManage/supplierManage'
import baseMixin from '@/mixins/base'
import { getDict } from '@/utils/index'
export default {
  mixins: [
    baseMixin({
      idName: 'id',
      findData: dataList,
      dataName: ['content', 'totalRecords'],
      addData: addData,
      updateData: updateData,
      deleteData: deleteData,
      isAdd: false,
      findDataById: details,
      format(res) {
        return res.map((data) => {
          data.businessHours = `${data.startTime}-${data.endTime}`
          if (data.status) {
            data.statusValue = '正常'
          } else {
            data.statusValue = '停用'
          }
          return data
        })
      },
    }),
  ],
  data() {
    return {
      authConfig: {
        query: 'supplier:jp:page',
        add: 'supplier:jp:create',
        edit: 'supplier:jp:update',
        del: 'supplier:jp:delete',
        status: 'supplier:jp:status',
      },
      form: {
        supplierType: '精品供应商',
        supplierName: '',
        storeId: '',
      },
      tableColumnAttributes: [
        { name: '所属门店', value: 'storeName', type: 'text', width: '200' },
        {
          name: '供应商编号',
          value: 'supplierCode',
          type: 'text',
          width: '200',
        },
        {
          name: '供应商名称',
          value: 'supplierName',
          type: 'text',
          width: '200',
        },
        {
          name: '供应商类别',
          value: 'supplierType',
          type: 'text',
          width: '200',
        },
        { name: '联系人', value: 'contactName', type: 'text', width: '100' },
        {
          name: '联系人电话',
          value: 'contactPhone',
          type: 'text',
          width: '130',
        },
        { name: '所属城市', value: 'cityName', type: 'text', width: '100' },
        { name: '所属区域', value: 'countyName', type: 'text', width: '100' },
        { name: '详细地址', value: 'address', type: 'text', width: '200' },
        { name: '主营业务', value: 'note', type: 'text', width: '200' },
        {
          name: '状态',
          type: 'switch',
          valueId: 'status',
          value: 'statusValue',
          width: '100',
          action: (row) => {
            changeStatus(row.id).then((res) => {
              this.search()
            })
          },
        },
        { name: '备注', value: 'remark', type: 'text', width: '200' },
        { name: '创建时间', value: 'createTime', type: 'text', width: '200' },
      ],
    }
  },
  created() {
    this.searchOptions = {
      supplierType: '精品供应商',
    }
  },
  methods: {
    search() {
      this.searchOptions = this.form
      this.currentPage = 1
      this.setTableData()
    },
    add() {
      this.$router.push({ path: '/supplierManage/addGoodSupplier' })
    },
    handleDataUpdateEdit(row, type) {
      this.$router.push({
        path: '/supplierManage/editGoodSupplier',
        query: { id: row.id },
      })
    },
  },
}
</script>
